package struts.action.departement;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import metier.Candidature;
import metier.Etudiant;
import metier.Departement;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import dao.DAOCandidature;
import dao.DAOEtudiant;

public class ActionCandidaturesDuDepartement extends Action {
	
	private DAOCandidature DAOCand = null;
	private DAOEtudiant DAOEtud = null;

	public DAOEtudiant getDAOEtud() {
		return DAOEtud;
	}

	public void setDAOEtud(DAOEtudiant dAOEtud) {
		DAOEtud = dAOEtud;
	}

	public DAOCandidature getDAOCand() {
		return DAOCand;
	}

	public void setDAOCand(DAOCandidature dAOCand) {
		DAOCand = dAOCand;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
					throws Exception 
	{	
		try
		{
			HttpSession mSession = request.getSession();
			
			Departement mDepartement = (Departement) mSession.getAttribute("DEPARTEMENT");
			
			List<Etudiant> listeEtudiants = DAOEtud.load(mDepartement, "", "", "", "");
			
			List<Candidature> listeCandidatures = new ArrayList<Candidature>();
					
			// chargement de toutes les candidatures
			for (Etudiant mEtudiant : listeEtudiants)
			{
				for (Candidature mCandidature : mEtudiant.getListeCandidatures())
				{
					if (mCandidature.getValidationDepartement() == null)
						listeCandidatures.add(mCandidature);
				}				
			}

			request.setAttribute("listeCandidatures", listeCandidatures);

			return mapping.findForward("CandidaturesDuDepartement");
		}
		catch (Exception mException)
		{
			ActionErrors errors = new ActionErrors();
			errors.add("Erreur interne. Veuillez vous reconnecter.", new ActionMessage("departement.erreur.erreurInterne"));
			saveErrors(request, errors);
			return mapping.findForward("ERREUR");
		}
	}

}